(function ($) {
	var reasonIds=''
	var prePageId = ''
	var sourceData
	$.init();
	$.plusReady(function() {
		baseSetup();
	})
	if (!mui.os.plus) {
		baseSetup();
	}
	
	//获取病区护士列表
	function getResingReason(){
		var pdict = {'type':'离职原因'}
		var paramDict = {'method':'web.INMDBComm:FindPubCode','params':JSON.stringify(pdict),'type':'Query'}
		//console.log(JSON.stringify(pdict))
		mui.DHCWebService(paramDict, function(result) {
			//console.log(JSON.stringify(result))
			var dataArr = result['data']
			sourceData = dataArr
			updateOptionsUL(dataArr)
		}, function(errorStr) {
			mui.toast(errorStr)
		});
	}
	function updateOptionsUL(dataArr){
		var reasonUL = $('#reasonUL')[0];
		reasonUL.innerHTML = ''
		var html = ''
		for (var i = 0; i < dataArr.length;i++) {
			var oneData = dataArr[i]
			var active = ''
			if (reasonIds.indexOf('~'+oneData['SubValue']+'~')!= -1) {
				active = 'dhc-active'
			}
			html += '<li id="'+i+'" class="mui-table-view-cell '+active+'"><div class="square"></div>'+
				'<div class="nurName mui-ellipsis">'+oneData['SubDesc']+'</div></li>'
		}
		reasonUL.innerHTML = html
	}
	
	function selectOneReason(){
		if (this.getAttribute('class').indexOf('dhc-active') != -1) {
			this.classList.remove('dhc-active')
			var selAll = $('#selAll')[0]
			if (selAll.getAttribute('class').indexOf('dhc-active') != -1) {
				selAll.classList.remove('dhc-active')
			}
		}else{
			this.classList.add('dhc-active')
		}
	}
	function selAllBtnTapped(){
		var isAll = false
		if (this.getAttribute('class').indexOf('dhc-active') != -1) {
			this.classList.remove('dhc-active')
		}else{
			this.classList.add('dhc-active')
			isAll = true
		}
		var lis = $('#reasonUL')[0].querySelectorAll('li');
		for (var i = 0; i < lis.length;i++) {
			var liDom = lis[i]
			if (isAll) { //全选
				liDom.classList.add('dhc-active')
			}else{
				liDom.classList.remove('dhc-active')
			}
		}
	}

	function baseSetup(){
		$('.mui-scroll-wrapper').scroll({
			bounce: true,
			indicators: false, //是否显示滚动条
		});
		var self = plus.webview.currentWebview()
		reasonIds = '~'+self.reasonIds+'~'
		prePageId = self.prePageId
		mui('body').on('tap','#closeBtn',mui.back)//关闭按钮
		mui('body').on('tap','#selAll',selAllBtnTapped); //全选按钮
		mui('#reasonUL').on('tap','li',selectOneReason)//关闭按钮
		mui('body').on('tap','#submitBtn',submitBtnTapped); //确定按钮
		getResingReason();
	}
	
	function submitBtnTapped(){
		var prePage = plus.webview.getWebviewById(prePageId)
		var lis = $('#reasonUL')[0].querySelectorAll('li.dhc-active');
		var ids = ''
		var descs = ''
		for (var i = 0; i < lis.length;i++) {
			var liDom = lis[i]
			if (i > 0) {
				ids +='~'
				descs += ';'
			}
			var index = liDom.id
			ids +=sourceData[index]['SubValue']
			descs += sourceData[index]['SubDesc']
		}
		var dict = {'ids':ids,'desc':descs}
		prePage.evalJS("updateResignReason('"+ids+"','"+descs+"')")
		mui.back()
	}
	
	
})(mui)
